<?php
require_once 'pages/TMSBackOfficePage.inc';
require_once 'models/lists.inc';

class TMSPageItem implements IItem {
	protected $base;
/**
 * @var TMSAdminPage
 */	
	protected $page;
	
	public function __construct(IItem $base, TMSAdminPage $page){
		$this->base = $base;
		$this->page = $page;
	}
	
	public function __get($nm){
		switch ($nm){
			case 'DeleteLink':return $this->page->ItemDeleteUrl($this->base);break;
			case 'EditLink':return $this->page->ItemEditUrl($this->base);break;
			case 'OpenLink':return $this->page->ItemOpenUrl($this->base);break;
			case 'ActionUrls':return $this->page->ActionUrls($this->base);break;
			default:return $this->base->$nm;break;
		}
	}
	
	public function ItemId(){
		return $this->base->ItemId();
	}
	
	public function ClassName(){
		return $this->base->ClassName();
	}
	
	public function __toString(){
		return (string)$this->base;
	}
	
	public function Properties($includetypes = array(),$excludetypes = array()){
		return array_uintersect($this->base->Properties($includetypes, $excludetypes), $this->page->DisplayProperties,create_function('$a,$b','return strcmp(($a instanceof IPropertyMeta)?$a->Name():(string)$a,($b instanceof IPropertyMeta)?$b->Name():(string)$b);'));
	}
	
	public function Property($name){
		return $this->base->Property($name);
	}
	
	public function Contents(){
		return new TMSPageItemIterator($this->base->Contents(),$this->page);
	}
}

class TMSPageItemIterator extends TIteratorAdapter {
/**
 * @var TMSAdminPage
 */	
	protected $page;
	
	public function __construct(IIterator $base, TMSListPage $page){
		parent::__construct($base);
		$this->page = $page;
	}
	
	public function Item(){
		$item = $this->iterator->Item();
		if ($item)
			return new TMSPageItem($item, $this->page);
		return null;
	}
}

interface IMSAdminPageModule {
	public function Caption();
}


/**
 * @author dkrasilnikov@gmail.com
 * @property string $ItemsClass
 * @property array $Path
 * @property IListProvider $Model
 */

class TMSAdminPage extends TMSBackOfficePage {
/**
 * @var IListProvider
 */	
	protected $_ioc_model_;
	
	protected $modules = array();
		
	protected $displayProperties = array();
	protected $hideProperties = array();
	
	private $_display_properties_ = null;
	
	public function RegisterComponent(IConfigurable $component){
		parent::RegisterComponent($component);
		if ($component instanceof IMSAdminPageModule && $component instanceof TWidget)
			$this->modules[$component->Name()] = $component;
	}
	
	public function Modules(){
		return $this->modules;
	}
	
	public function __set($nm,$value){
		switch ($nm){
			case 'DisplayProperties':$this->displayProperties = TConvertions::ConvertToArray($value);break;
			case 'HideProperties':$this->hideProperties = TConvertions::ConvertToArray($value);break;
			default:parent::__set($nm,$value);break;
		}
	}
	
	public function __get($nm){
		switch ($nm){
			case 'ItemsClass':return $this->Model->ClassName();break;
			case 'DisplayProperties':{
				if (is_null($this->_display_properties_)){
					$f = create_function('$a,$b','return strcmp(($a instanceof IPropertyMeta)?$a->Name():(string)$a,($b instanceof IPropertyMeta)?$b->Name():(string)$b);');
					if (empty($this->displayProperties))
						$temp = $this->Model->ClassProperties();
					else
						$temp = array_uintersect($this->Model->ClassProperties(),$this->displayProperties,$f);
					$this->_display_properties_ = array_udiff($temp,$this->hideProperties,$f);		
				}	
				return $this->_display_properties_;
			}break;
			case 'Path':return array();break;
			default:return parent::__get($nm);break;
		}
	}
	
	public function Handle(THttpRequest $request){
		if ($dp = $request->Cookie($this->Name().'_dp')){
			$this->DisplayProperties = $dp;
			$this->SetCookie($this->Name().'_dp', join(';',array_keys($this->DisplayProperties)),time()+ 2592000);
		}
		return parent::Handle($request);
	}	
	
	public function http_SetDisplayProperties(array $display = array()){
		$this->SetCookie($this->Name().'_dp', empty($display)?null:join(';',array_keys($display)),time()+ 2592000);
	}
	
	public function http_Delete($id){
		return $this->Model->DeleteItem($id);
	}
	
	public function ItemDeleteUrl(IItem $item){
		return $this->Url(null,'Delete',array('id'=>$item->ItemId()));
	}
	
	public function ItemEditUrl(IItem $item){
		return $this->service->ItemEditUrl($item,array());
	}

	public function CollectionItemUrl(IItem $item, IItem $container){
		return $this->service->ItemEditUrl($item,array('container'=>$container->ItemId()));
	}	
	
	public function ItemOpenUrl(IItem $item){
		return $this->service->ItemNavigationUrl($item);
	}
	
	public function PropertyFieldName(IProperty $p){
		return $p->Name();
	}
	
	public function ActionUrls(IItem $item){
		return array(
			'Edit'=>$this->ItemEditUrl($item),
			'Delete'=>$this->ItemDeleteUrl($item)
		);
	}
}